package com.party.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.party.domain.Collect;
import com.party.domain.Custom;
import com.party.domain.Orders;
import com.party.domain.User;
import com.party.dto.Response;
import com.party.service.impl.ShoppingServiceImpl;

@Controller
@RequestMapping("/shopping")
public class ShoppingController 
{
	final Logger logger = LoggerFactory.getLogger(ProductController.class);
	
	@Autowired
	private	ShoppingServiceImpl shservice;
	
	@Autowired
	HttpServletResponse response;
	
	@Autowired
	HttpServletRequest request;
	
	/*
	 * 订单模块
	 * 
	 * */
	
	//添加订单和订单详情
	@RequestMapping(value="/addorder")
	@ResponseBody
	public Response addOrder(int product_id,int product_number,HttpSession session)
	{
		try {
			SecurityContext context = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
		    User user = (User) context.getAuthentication().getPrincipal();
		    int uid=user.getId();
		    logger.info("增加订单中");
		    int oid=shservice.addOrder(uid);
		    logger.info("增加订单成功");
			
		    System.out.println(oid+"   "+product_id+"     "+product_number);
		    logger.info("增加订单详情中");
		    shservice.addOrderDetail(oid, product_id, product_number);
		    logger.info("增加订单详情成功");
			return Response.success();
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	//删除订单和订单详情
	@RequestMapping(value="/deleteorder")
	@ResponseBody
	public Response deleteOrder(int oid)
	{
		try {
			logger.info("删除订单详情中");
		    shservice.deleteOrderDetail(oid);
		    logger.info("删除订单详情成功");
			
		    logger.info("删除订单中");
		    shservice.deleteOrder(oid);
		    logger.info("删除订单成功");

		    return Response.success();
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	//分页获取用户所有订单
	@RequestMapping(value="getorders")
	@ResponseBody
	public Response getOrders(int page,int size,HttpSession session)
	{
		try {
			SecurityContext context = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
		       User user = (User) context.getAuthentication().getPrincipal();
		       List<Orders> orders=shservice.findByUidOrders(user.getId(), page, size);
		       
			return Response.success(orders);
			
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	//获取订单商品信息
	@RequestMapping(value="getordersdetail")
	@ResponseBody
	public Response getOrderDetail(int oid)
	{
		try {
			Orders orders=shservice.fidByOidOrders(oid);
			return Response.success(orders);
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	//获取未支付订单
	@RequestMapping(value="getNoStateOrders")
	public String getNoStateOrders(int page,int size,Model model,HttpSession session) throws Exception
	{
		SecurityContext context = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
	       User user = (User) context.getAuthentication().getPrincipal();
	       List<Orders> orders=shservice.findByNoStateOrders(user.getId(), page, size);
	       model.addAttribute("nostate",orders);
	       return "NoStateOrders";
	}
	
	
	
	/*
	 * 收藏模块
	 * 
	 * */
	
	//添加商品收藏
	@RequestMapping(value="addcollect")
	@ResponseBody
	public Response addCollect(Collect collect,HttpSession session)
	{
		try {
			SecurityContext context = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
		       User user = (User) context.getAuthentication().getPrincipal();
		       collect.setUser_id(user.getId());
			
			 logger.info("增加订商品收藏中");
			 if(shservice.addCollect(collect)==false)
			 {
				 return Response.failure("您已收藏过该商品");
			 }
			 
			 logger.info("增加商品收藏成功");
			
			return Response.success();
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	
	//删除收藏商品
	@RequestMapping(value="deletecollect")
	@ResponseBody
	public Response deleteCollect(int coid)
	{
		try {
			
			  logger.info("删除收藏中");
			  shservice.deleteCollect(coid);
			  logger.info("删除收藏成功");	
			return Response.success();
		} catch (Exception e) {
			
			return Response.failure();
		}
	}
	
	//按收藏id获取收藏商品信息
	@RequestMapping(value="getcoidpro")
	@ResponseBody
	public Response getCoidProduct(int coid)
	{
		try {
			Collect collect =shservice.findByCoidProdoct(coid);
			return Response.success(collect);
		} catch (Exception e) {
			return Response.failure();
		}
		
	}
	
	//获取用户所有收藏
	@RequestMapping(value="getcollect")
	@ResponseBody
	public Response getCollect(int page,int size,HttpSession session)
	{
		try {
			SecurityContext context = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
		       User user = (User) context.getAuthentication().getPrincipal();
			List<Collect> collect =shservice.fidByUidProduct(user.getId(), page, size);
			return Response.success(collect);
		} catch (Exception e) {
			return Response.failure();
		}
		
	}
	
	
	/*
	 * 定制服务模块
	 * 
	 * */
	
	//添加定制服务
	@RequestMapping(value="addcustom")
	@ResponseBody
	public Response addCustom(Custom custom,HttpSession session)
	{
		try {
			SecurityContext context = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
		    User user = (User) context.getAuthentication().getPrincipal();
		    custom.setCu_uid(user.getId());
		    logger.info("增加定制服务中");
			shservice.addCustom(custom);
			logger.info("增加定制服务成功");
			return Response.success();
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	//按id查询定制服务
	@RequestMapping(value="/getIdCustom")
	@ResponseBody
	public Response getIdCustom(int cuid)
	{
		try {
			 logger.info("id查询定制服务中");
			 Custom custom=shservice.findIdCustom(cuid);
			 logger.info("id查询定制服务成功");
			return Response.success(custom);
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	
	//按id删除定制服务
	@RequestMapping(value="/deletecustom")
	@ResponseBody
	public Response deleteCustom(int cuid)
	{
		try {
			logger.info("删除定制服务中");
		    shservice.deleteCustom(cuid);
		    logger.info("删除定制服务成功");
			
			
			return Response.success();
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	//分页查询所有定制服务
	@RequestMapping(value="/findallcustom")
	@ResponseBody
	public Response findAllCustom(int page,int size)
	{
		try {
			
			List<Custom> custom=shservice.findAllCustom(page, size);
			
			return Response.success(custom);
		} catch (Exception e) {
			return Response.failure();
		}
	}
	
	//按手机号查询所有定制服务
	@RequestMapping(value="/findphonecustom")
	@ResponseBody
	public Response findPhoneCustom(String phone,int page,int size)
	{
		try {
			List<Custom> custom=shservice.findPhoneCustom(phone, page, size);
			return Response.success(custom);
		} catch (Exception e) {
			return Response.failure();
		}
	}
	

	

	

	
	
}
